<?php
class Controller_CPA extends  Controller_Template 
{
        public function __construct($req, $res) 
        {
		$this->template = "smarty:cpa/daily";
		parent::__construct($req, $res);
	}

        public function action_index ()
        {
            $this->request->redirect('/cpa/stats');
        }
        
        public function action_import()
        {
            $post = $this->request->post();
            if (! empty($post))
            {
                $appfile  = Arr::get($_FILES, 'cpaexcel');
                $filename  = Arr::get($appfile, 'name');
                $extend=strrchr ($filename,'.');
                if($extend!='.xls')
                {
                    throw new Exception("请导入Excel 2003格式文件……");
                }
                $filesize = Arr::get($appfile, 'size', 0);
                if($filesize<=0||$filesize>20*1024*1024)
                {
                   throw new Exception("文件不能为空，超过20M");
                }
                $arr = CPA::import($appfile);
                $this->request->redirect('/cpa/stats');
            }
            $this->template->set_filename('smarty:cpa/import');
        }

        public function action_stats($days = NULL)
        {
            $start_date = $_GET['start_date'];
            $end_date   = $_GET['end_date'];
//            if($start_date==""||$end_date=="")
//            {
//                $start_date = date("Y/m/d", time() - 86400 * 7);
//                $end_date   = date("Y/m/d", time());
//            }
            if($days == NULL){
                $end_date  = empty($end_date)?date('Ymd', time()):date('Ymd', strtotime($end_date));
                $start_date = empty($start_date)?date('Ymd', time()-86400*7):date('Ymd', strtotime($start_date));
            }  else {
                $end_date  = empty($end_date)?date('Ymd', time()):date('Ymd', strtotime($end_date));
                $start_date = empty($start_date)?date('Ymd', time()-86400*$days+86400):date('Ymd', strtotime($start_date));
            }
            
            list($day_start1, $day_end1) = $this->build_days($start_date, $end_date);
            $records = CPA::stats($day_start1, $day_end1);
            $this->template->set('start_date',date('Y/m/d', strtotime($start_date)));
            $this->template->set('end_date',date('Y/m/d', strtotime($end_date)));
            $this->template->set('records', $records);
            $this->template->set_filename('smarty:cpa/stats');
        }

	    public function action_daily($day_start = NULL, $day_end = NULL)
	    {
            list($day_start, $day_end) = $this->build_days($day_start, $day_end);
            $records = CPA::records($day_start, $day_end);
            $this->template->set('day_start', $day_start);
            $this->template->set('day_end', $day_end);
            $this->template->set('records', $records);
	    }

        private function build_days($day_start, $day_end)
        {
                $day_start = date("Y-m-d", strtotime($day_start));
                $day_end = date("Y-m-d", strtotime($day_end));
                return array($day_start, $day_end);
        }


}
